package org.peterf.foxish.component.updater;

import org.peterf.foxish.component.property.Updatable;
import org.peterf.foxish.component.updater.interfaces.GradualReversable;

public class AlphaUpdater implements GradualReversable {

    private float endAlpha;

    private float progress = 0.0f;

    private boolean reversed = false;

    private float startAlpha;

    public AlphaUpdater(float startAlpha, float endAlpha) {
        this.startAlpha = startAlpha;
        this.endAlpha = endAlpha;
    }

    public float getProgress() {
        return progress;
    }

    public boolean isFinished() {
        return progress >= 1.0f;
    }

    public boolean isReversed() {
        return reversed;
    }

    public void reset() {
        progress = 0.0f;
        reversed = false;
    }

    public void reset(float startAlpha, float endAlpha) {
        this.startAlpha = startAlpha;
        this.endAlpha = endAlpha;
        reset();
    }

    public void reverse() {
        reversed = !reversed;
        progress = 0.0f;
    }

    public void setProgress(float progress) {
        this.progress = progress;
    }

    public void update(Updatable updatable, float elapsed) {
        if (reversed) {
            updatable.setAlpha(startAlpha * progress + endAlpha
                    * (1.0f - progress));
        } else {
            updatable.setAlpha(startAlpha * (1.0f - progress) + endAlpha
                    * progress);
        }
    }

}
